package main

import (
	"fmt"
	"math"
	"sync"
	"time"
)

var wg08 sync.WaitGroup
var chSem chan int

func main() {

	//semaphore信号量
	chSem = make(chan int, 5)

	for i := 0; i < 100; i++ {
		wg08.Add(1)
		go getSqrt(i)
	}

	wg08.Wait()
}

func getSqrt(n int) {
	//规定:所有并发任务都必须注册在信号量管道里
	chSem <- n

	fmt.Printf("%d的平方根是%.2f\n", n, math.Sqrt(float64(n)))
	<- time.After(10 * time.Second)

	//任务结束后从信号量管道注销,给其它腾出空间
	<-chSem
	wg08.Done()
}
